﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace LeanEngine.Data.Linq.Language
{
    public sealed class WhereToken : IQueryToken
    {
        #region Constructor Members

        public WhereToken()
        {
            this.PredicateList = new List<CompareToken>();
        }

        #endregion

        #region PropertyInfo Members

        public List<CompareToken> PredicateList { get; set; }

        #endregion

        #region ILanguageToken Members

        public string GetStatement()
        {
            if (this.PredicateList.Count == 0) return string.Empty;

            var sql = new StringBuilder();
            sql.Append("WHERE ");
            
            for(int i = 0 ; i < PredicateList.Count; i++)
            {
                if((i + 1) >= PredicateList.Count)
                {
                    PredicateList[i].CombineOperator = string.Empty;
                }

                sql.Append(PredicateList[i].GetStatement());
                sql.Append("\r\t");
            }

            return sql.ToString();
        }

        #endregion
    }
}
